package db

import (
	"fmt"

	"github.com/jmoiron/sqlx"
	_ "github.com/lib/pq"
)

type Config struct {
  Host     string
  Port     string
  Username string
  Password string
  DBName   string
  SSLMode  string
}

func NewPostgresDB(cfg Config) (*sqlx.DB, error) {
  db, err := sqlx.Connect("postgres", fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s sslmode=%s",
      cfg.Host, cfg.Port, cfg.Username, cfg.DBName, cfg.Password, cfg.SSLMode))

  if err != nil {
      return nil, err
  }

  err = db.Ping()
  if err != nil {
    return nil, err
  }

  db.MustExec(schema)

  return db, nil
}

